<?php
class Account_log_model extends BZ_Model
{
    protected $table = 'account_log';
    
    public function findByUid($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }
    
    public function total($params=array())
    {
        if (!empty($params['uid'])) {
            $this->db->where('uid', $params['uid']);
        }
        if (!empty($params['order_id'])) {
            $this->db->where('order_id', $params['order_id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('date >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('date <=', $params['endData'].' 23:59:59');
        }
        if ( ! empty($params['product_type'])) {
            $this->db->where('product_type', $params['product_type']);
        }
        if ( ! empty($params['type'])) {
             if (is_array($params['type'])) {
                 $this->db->where_in("type", $params['type']);
            } else {
                 $this->db->where("type", $params['type']);
            }
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $params=array())
    {
        $this->db->select('`log_id`, `order_id`, `uid`, `amount`, `amount_carry`, `date`, `type`, `account_type`, `product_type`, `remittance_person`, `note`, LEFT(`date`, 7) AS month', FALSE);
        if (!empty($params['uid'])) {
            $this->db->where('uid', $params['uid']);
        }
        if (!empty($params['order_id'])) {
            $this->db->where('order_id', $params['order_id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('date >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('date <=', $params['endData'].' 23:59:59');
        }
        if ( ! empty($params['product_type'])) {
            $this->db->where('product_type', $params['product_type']);
        }
        if ( ! empty($params['type'])) {
            if (is_array($params['type'])) {
                 $this->db->where_in("type", $params['type']);
            } else {
                 $this->db->where("type", $params['type']);
            }
        }
        $this->db->from($this->table);
        $this->db->order_by('log_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    

    /**
     * 账户记录
     * @param unknown $data
     */
    public function insertAccountLog($params)
    {
        $data = array(
            'order_id'     => 0,
            'uid'          => $params['uid'],
            'amount'       => $params['deduction'],
            'amount_carry' => $params['amount_carry'],
            'date'         => date('Y-m-d H:i:s', time()),
            'note'         => '',
            'type'         => DEDUCTION, //DEDUCTION
            'account_type' => ACCOUNT_TYPE_CARRY, //ACCOUNT_TYPE_CARRY
            'product_type' => 'tourism',
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function insertBatchAccountLog($postData)
    {
        $data = array();
        foreach ($postData as $value) {
            $data[] = array(
                'order_id'     => $value->order_id,
                'uid'          => $value->uid,
                'amount'       => $value->account,
                'date'         => date('Y-m-d H:i:s', time()),
                'type'         => $value->type,
                'account_type' => $value->account_type,
                'product_type' => 'tourism',
            );
        }
        return $this->db->insert_batch($this->table, $data);
    }
    
    public function insertBatch($postData)
    {
        if (empty($postData)) {
            return false;
        }
        return $this->db->insert_batch($this->table, $postData);
    }
}